package org.yray.sip.utils;

import cn.hutool.core.util.HexUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class IdUtils {

    public static String getId()
    {
       return String.format("%d",System.currentTimeMillis());
    }

    public static long  generateSsrc(Object o) {
        Random random = new Random(System.nanoTime() + Thread.currentThread().getId()
                - Thread.currentThread().hashCode() + o.hashCode());
        long ssrc = random.nextInt();
        while (ssrc > Math.pow(2,31))
        {
            ssrc = random.nextInt();
        }
        if(ssrc < 0) {
            ssrc = ssrc * -1;
        }
        return ssrc;
    }
    public static String  generateCname(Object o) {
        Random random = new Random(System.currentTimeMillis() + Thread.currentThread().getId()
                - Thread.currentThread().hashCode() + o.hashCode());
        long val = random.nextLong();

        if(val < 0) {
            val = val * -1;
        }

        return String.format("%s",HexUtil.toHex(val));
    }
    public static int  generateSeq(Object o) {
        Random random = new Random(System.currentTimeMillis() + Thread.currentThread().getId()
                - Thread.currentThread().hashCode() + o.hashCode());
        int ssrc = random.nextInt();
        if(ssrc < 0) {
            ssrc = ssrc * -1;
        }
        return ssrc;
    }
    public static void main(String[] args) {
        log.info(getId());
        log.info("{}",Math.pow(2,31));
    }
}
